package timing.ukulele.portal.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import timing.ukulele.portal.mapper.SysDictIndexMapper;
import timing.ukulele.portal.persistent.SysDict;
import timing.ukulele.portal.persistent.SysDictIndex;

import java.util.Date;

@Service
public class SysDictIndexService extends BaseOperatorService<SysDictIndexMapper, SysDictIndex> {
    public IPage<SysDictIndex> getPage(String key, String name, String remark, Boolean deleted, int current, int size) {
        Page<SysDictIndex> page = new Page<>(current, size);
        LambdaQueryWrapper<SysDictIndex> query = new LambdaQueryWrapper<>();
        query.like(StringUtils.hasText(key), SysDictIndex::getKey, key);
        query.like(StringUtils.hasText(name), SysDictIndex::getName, name);
        query.like(StringUtils.hasText(remark), SysDictIndex::getRemark, remark);
        query.eq(deleted != null, SysDictIndex::getDeleted, deleted);
        query.orderByDesc(SysDictIndex::getId);
        IPage<SysDictIndex> iPage = getBaseMapper().selectPage(page, query);
        return page.setRecords(iPage.getRecords());
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean deleteDictIndex(SysDictService dictService, Long id, String currentUser) {
        Date now = new Date();
        this.lambdaUpdate().set(SysDictIndex::getDeleted, Boolean.TRUE)
                .set(SysDictIndex::getUpdateBy, currentUser).set(SysDictIndex::getUpdateTime, now)
                .eq(SysDictIndex::getId, id).update();
        dictService.lambdaUpdate().set(SysDict::getDeleted, Boolean.TRUE)
                .set(SysDict::getUpdateBy, currentUser).set(SysDict::getUpdateTime, now).eq(SysDict::getIndexId, id).update();
        return true;
    }

}
